package problem1302;

//1302.层数最深叶子节点的和
//https://leetcode.cn/problems/deepest-leaves-sum/

class Solution {
    public int maxheight(TreeNode root) {
        if(root == null) return 0;
        int left = maxheight(root.left);
        int right = maxheight(root.right);
        return Math.max(left, right) + 1;
    }
    public int height;
    public int sum = 0;
    public int deepestLeavesSum(TreeNode root) {
        height = maxheight(root);
        dfs(0, root);
        return sum;
    }
    public void dfs(int u, TreeNode root) {
        if(root == null) return;
        if(u == height-1) {
            sum += root.val;
        }
        dfs(u+1, root.left);
        dfs(u+1, root.right);
    }
}